package org.shareworth.online_life.client.util;

import java.util.List;
import java.util.Set;

import com.extjs.gxt.ui.client.core.TemplatesCache.Cache.Key;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.URL;
import com.google.gwt.http.client.RequestBuilder.Method;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

/**
 * JSON数据请求
 * 
 * @author 拉图De守望 2009-11-27
 */
public class JSONRequest {

	private String url;
	private String method;
	private String requestData;
	private RequestCallback requestCallback;

	/**
	 * 
	 * @param url
	 *            请求URL
	 * @param method
	 *            请求方法 GET 或POST 默认为GET
	 * @param requestData
	 *            请求参数
	 * @param requestCallback
	 *            回应操作
	 */
	public JSONRequest(String url, String method, String requestData,
			RequestCallback requestCallback) {
		super();
		this.url = url;
		this.method = method;
		this.requestData = requestData;
		this.requestCallback = requestCallback;
	}

	public JSONRequest(String url, String method, String requestData) {
		super();
		this.url = url;
		this.method = method;
		this.requestData = requestData;
	}

	public JSONRequest(String url, String requestData,
			RequestCallback requestCallback) {
		super();
		this.url = url;
		this.requestData = requestData;
		this.requestCallback = requestCallback;
	}

	public JSONRequest(String url, RequestCallback requestCallback) {
		super();
		this.url = url;
		this.requestCallback = requestCallback;
	}

	/**
	 * 请求参数为JSON数据格式
	 */
	public void setDataByJSON(JSONObject jSONObject) {
		StringBuilder json = new StringBuilder();
		Set<String> keys = jSONObject.keySet();
		for (String key : keys) {
			jsonToString(key, jSONObject.get(key), json);

		}

		this.requestData = json.toString();
	}

	private StringBuilder jsonToString(String key, JSONValue jValue,
			StringBuilder string) {

		JSONString jString = jValue.isString();

		if (jString != null) {
			String s = jString.toString();
			string.append(key + "=" + s.substring(1, s.length() - 1) + "&");
			return string;
		}
		JSONArray jArray = jValue.isArray();
		if (jArray != null) {
			for (int i = 0; i < jArray.size(); i++) {
				String s = jArray.get(i).toString();
				string.append(key + "=" + s.substring(1, s.length() - 1) + "&");
			}
		}

		return string;

	}

	/**
	 * 请求格式为FormPanel数据格式
	 * 
	 * @param formPanel
	 */
	public void setDataByFormPanel(FormPanel formPanel) {
		StringBuilder json = new StringBuilder();
		List<Field<?>> fields = formPanel.getFields();
		for (Field<?> field : fields) {
			json.append(field.getName() + "=" + field.getValue()+"&");
		}

		this.requestData = json.toString();
	}

	/**
	 * 发送请求
	 * 
	 * @throws RequestException
	 */
	public void sendRequest() throws RequestException {
		Method method = null;
		if (method != null && "POST".equals(this.method.toUpperCase()))
			method = RequestBuilder.POST;
		else {
			method = RequestBuilder.POST;
		}

		RequestBuilder builder = new RequestBuilder(method, (!url
				.contains("http://") ? GWT.getHostPageBaseURL() : null)
				+ URL.encode(url));
		//MessageBox.alert("", requestData, null);
		builder.setHeader("Content-Type", "application/x-www-form-urlencoded");

		builder.setRequestData(requestData);
		builder.setCallback(requestCallback);
		builder.send();

	}

}
